package com.naiterui.ehp.bs.doctor.service.record.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.domain.RecordUserInfo;
import com.naiterui.ehp.bs.doctor.domain.StandardDict;
import com.naiterui.ehp.bs.doctor.fegin.PharmacistFeignClient;
import com.naiterui.ehp.bs.doctor.repository.IStandardDictRepository;
import com.naiterui.ehp.bs.doctor.vo.record.PharmacistParamVO;
import com.naiterui.ehp.bs.doctor.vo.record.RecordInfoVO;
import com.naiterui.ehp.bs.doctor.vo.record.ReocrdUserBaseInfoVO;

/**
 * @author guoyongxiang
 * @date 2020/2/20 17:03
 * @since 1.0.0
 */
@Service
public class RecordInfoPharmacistServiceImpl extends AbstractRecordInfoServiceImpl {

    @Autowired
    private PharmacistFeignClient pharmacistFeignClient;
    @Autowired
    private IStandardDictRepository standardDictRepository;

    /**
     * 更新基础信息
     *
     * @throws BusinessException
     */
    @Override
    protected String updateBaseInfo(RecordInfoVO recordInfoVO, String operator) throws BusinessException {
        ReocrdUserBaseInfoVO baseInfoVO = recordInfoVO.getUserBaseInfo();
        PharmacistParamVO pharmacist = new PharmacistParamVO();

        pharmacist.setId(baseInfoVO.getUserId());
        if (StringUtils.isNotBlank(baseInfoVO.getName())) {
            pharmacist.setName(baseInfoVO.getName());
        }
        if (StringUtils.isNotBlank(baseInfoVO.getPhone())) {
            pharmacist.setPhone(baseInfoVO.getPhone());
        }
        if (StringUtils.isNotBlank(baseInfoVO.getIdCardNum())) {
            pharmacist.setCardNo(baseInfoVO.getIdCardNum());
        }

        if (StringUtils.isNoneBlank(recordInfoVO.getUserInfo().getNationCode())) {
            StandardDict standardDict = this.standardDictRepository.findByCodeAndType(recordInfoVO.getUserInfo()
                                                                                                  .getNationCode(), StandardDict.TYPE_NATION);
            pharmacist.setEthnicity(standardDict.getName());
        }

        if (baseInfoVO.getTitleId() != null) {
            pharmacist.setTitleId(baseInfoVO.getTitleId());
        }

        if (StringUtils.isNotBlank(baseInfoVO.getCertNum())) {
            pharmacist.setLicenseNo(baseInfoVO.getCertNum());
        }
        // 资格证
        if (baseInfoVO.getCertUrls() != null && baseInfoVO.getCertUrls().size() > 0) {
            pharmacist.setLicenseUrl(String.join(",", baseInfoVO.getCertUrls()));
        }
        if (baseInfoVO.getUserId() == null) {
            // 创建电子签名用户
            Long id = this.pharmacistFeignClient.add(operator, pharmacist);
            pharmacist.setId(id);
        } else {
            this.pharmacistFeignClient.update(operator, pharmacist);
        }

        return this.getUserCode(pharmacist.getId(), RecordUserInfo.USER_TYPE_PH);
    }

}
